<?php
class MakeExamModel extends Model
{
    function  __construct()
    {
        parent::__construct();        
    }
    
    public function save($classid, $subjectid, $day, $month, $year, $hour, $minute, $isReal)
    {
        // validation
        $check = true;
        $message = '';
        try{
            $tz = new DateTimeZone('Asia/Bangkok');
            $date = new DateTime("$year-$month-$day", $tz);
            $date->setTime($hour, $minute, 0);
            $today = new DateTime('now', $tz);
            if ($date<$today){
                $check = false;
                $message .= '<br /> - Ngày tháng thi phải sau thời điểm hiện tại.';
            }
            if ( ! checkdate($month, $day, $year)){
                $check = false;
                $message .= '<br /> - Ngày tháng không đúng.';
            }
        }
        catch (Exception $e) {
            $check = false;
            $message .= '<br /> - Thời gian thi không đúng.';
        }
        if ($hour<0 || $hour>24 || $minute<0 || $minute>59){
            $check = false;
            $message .= '<br /> - Thời gian thi không đúng.';
        }
        
        // redirect    
        if (!$check){
            $message = 'Có lỗi xảy ra. Hãy kiểm tra lại!' . $message;
            //$this->loadExamInfo($classid, $message);
        }
        else{
            $this->loadTable('examinfo');
            if ($this->checkExamInfoExists($classid)){
                $examInfo = array('time'=>"$year-$month-$day $hour:$minute:00", 'isreal'=>$isReal);
                $examInfoObj = new ExamInfoTable($examInfo);
                $condition = array('classid'=>$classid);
                $examInfoObj->update($condition);
                //var_dump($examInfoObj);
            }
            else{
                $examInfo = array('classid'=>$classid, 'time'=>"$year-$month-$day $hour:$minute:00", 'isreal'=>$isReal);
                $examInfoObj = new ExamInfoTable($examInfo);
                $examInfoObj->insert();
            }
            $db = DataProvider::getInstance();
            $db->query("UPDATE results SET finishtime = null WHERE classid = $classid");
            $r = $db->query("CALL randommakeexam('$subjectid');");
            $message = "Tạo đề cho lớp $classid thành công!";
            //$this->loadClassList(null, null, null, $message);
        }
        
        return array('check'=>$check, 'message'=>$message);
    }
    
    public function getClassList($id=null, $subjectName=null, $lectureName=null)
    {
        $db = DataProvider::getInstance();
        $rs = $db->query("SELECT c.id AS id, subjectid, s.name AS subject, u.name AS lecture, semester, time, isreal FROM classes AS c JOIN users AS u ON c.lectureid = u.id JOIN subjects AS s ON c.subjectid = s.id LEFT JOIN examinfo AS e ON c.id = e.classid WHERE c.id LIKE '%$id%' AND s.name LIKE '%$subjectName%' AND u.name LIKE '%$lectureName%' AND isfinished = 0");
        $objArr = array();
        while ($row = mysql_fetch_array($rs))
        {
            $objArr[] = new DataObject($row);
        }
        return $objArr;
    }

    public function getQuestions($subjectID)
    {

        $this->loadTable('tempexams');
        $tempExamsTableObj = new TempExamsTable();
        $condition = array('subjectID'=>$subjectID);
        $option = array('order'=>'rand()');
        $results = $tempExamsTableObj->get($condition, $option)->getAll();

        if (count($results)==0) return null;

        $questionArr = array();

        $this->loadTable('questions');
        $questionsTableObj = new QuestionsTable();

        foreach ($results as $result)
        {
            $condition = array('id'=>($result->questionid));
            $questions = $questionsTableObj->get($condition)->getAll();
            $questionObj = $questions[0];

            $this->loadTable('options');
            $optionsTableObj = new OptionsTable();
            $condition = array('questionid'=>($result->questionid));
            $options = $optionsTableObj->get($condition)->getAll();
            $questionObj->optionArr = $options;

            $questionArr[] = $questionObj;
        }

        return $questionArr;
    }
    
    private function checkExamInfoExists($classID)
    {
        $this->loadTable('examinfo');
        $examInfoTable = new ExamInfoTable();
        $condition = array('classid'=>$classID);
        $examInfo = $examInfoTable->get($condition)->getAll();
        if (count($examInfo)) return true;
        else return false;
    }
}  
?>